<template>
	<view :class="['page',{'iphoneX':isIphoneX}]">  
		<view class="body"> 
			<template > 
				<view class="goods-wrap" v-for="(item,index) in form.goods" :key="index">
					<view class="hd-title">
						<u--text class="" :text="'货物清单('+(index+1)+')'" size="30" align="left" bold></u--text>
						<u-icon v-if="index>0" name="trash" :size="42" color="#ff0000" @click="delGoods(index)"></u-icon>
					</view>				
					<u--form :model="form" ref="uForm" class="form-wrap"> 
						<u-form-item label="货物类别" labelWidth="160" prop="categoryname" borderBottom="true" class="u-form-item" @click="form.goodsIndex=index;select('category')">  <!-- pcategory.isShow=true;form.goodsIndex=index -->
							<u-input v-model="item.categoryname" placeholder="选择货物类别" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
						<u-form-item v-if="item.categoryname" label="货物名称" labelWidth="160" prop="pname" borderBottom="true" class="u-form-item" @click="form.goodsIndex=index;select('product')">    <!-- pnames.isShow=true;form.goodsIndex=index" -->
 							<u-input v-model="item.pname" placeholder="选择货物名称" disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<text v-if="item.one_price=='0'">(赠送)</text>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
						<template v-if="item.stocks.length>0&&item.pname" >
							<view v-if="userInfo.role_id!=24" class="store-info">
								<view v-for="(subitem,subindex) in item.stocks" :key="subindex" class="row-2">
									<u--text class="text-label" :text="subitem.sname"  size="30" align="left" ></u--text>
									<u--text class="text-unit" :text="Math.floor(subitem.count)+'/'+subitem.uname" size="30" align="right" ></u--text>
								</view>								
							</view>
							<view class="row-2">
								<u-form-item label="出库数量" labelWidth="160" prop="unit" borderBottom="true" class="u-form-item num-inp-bar"> 
									<u-input type="digit" class="text-inp" v-model="item.p_count"  border="none" placeholder="输入出库数量"/>
									<u--text class="text-unit" :text="item.unit" size="30" align="right" ></u--text>
								</u-form-item>
								<u-button text="选择单位" type="success" size="small" class="btn m-l2" style="width: 100rpx;" @click="units.isShow=true;form.goodsIndex=index"></u-button>
							</view>
							<view class="row-2">
								<u-form-item label="产品单价" labelWidth="160" prop="unit" borderBottom="true" class="u-form-item num-inp-bar"> 
									<u-input type="digit" v-model="item.one_price" class="text-inp" border="none" placeholder="输入产品单价" @blur="setPrice"/>
									<u--text class="text-unit" text="元" size="30" align="right" ></u--text>
								</u-form-item> 
							</view>
						</template>						
					</u--form>
				</view>				
			
			
				<view class="m-t2 m-l2 m-r2 m-b2">
					<u-button  type="primary" :plain="true" :hairline="true" text="增加货物" @click="addGoods"></u-button>
				</view>			
				
				<view class="goods-wrap m-b2">
					<view class="hd-title">
						<u--text class="" text="其他信息" size="30" align="left" bold></u--text> 
					</view>
					<u--form :model="form" ref="uForm" class="form-wrap"> 
						<u-form-item label="项目名称" labelWidth="160" prop="proName" borderBottom="true" class="u-form-item" @click="projectlist.isShow=true">
							<u-input v-model="form.proName" placeholder="选择项目名称" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
						<u-form-item label="客户名称" labelWidth="160" prop="customer" borderBottom="true" class="u-form-item">
							<u-input v-model="form.customer" placeholder="输入客户名称"  border="none" />		
						</u-form-item> 
						<template v-if="form.company_val_name!='其他'">
							<u-form-item label="销售公司名称" labelWidth="240" prop="company_val_name" borderBottom="true" class="u-form-item" @click="company.isShow=true">
								<u-input v-model="form.company_val_name" placeholder="选择销售公司名称" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
								<u-icon	slot="right" name="arrow-right"></u-icon>	
							</u-form-item>
						</template>
						
						<template v-else>
							<u-form-item label="销售公司名称" labelWidth="240" prop="company_val_name" borderBottom="true" class="u-form-item"> 
								<u-input v-model="form.company_name" placeholder="输入销售公司名称"  border="none" />
								<u-icon	slot="right" name="close-circle" class="f-red" @click="form.company_val_name=''"></u-icon>	
							</u-form-item>
						</template>
						
						<u-form-item label="总货款" labelWidth="160" prop="money" borderBottom="true" class="u-form-item">
							<u-input type="digit" v-model="form.money" placeholder="输入总货款"  border="none" /> 
							<u--text class="text-unit" text="元" size="30" align="right" ></u--text>				
						</u-form-item>
						<u-form-item label="是否结清" labelWidth="160" prop="payStatus_name" borderBottom="true" class="u-form-item" @click="payStatus.isShow=true">
							<u-input v-model="form.payStatus_name" placeholder="是否结清" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
						<u-form-item label="已结款" labelWidth="160" prop="isPay" borderBottom="true" class="u-form-item">
							<u-input type="digit" v-model="form.isPay" placeholder="输入已结款"  border="none" /> 
							<u--text class="text-unit" text="元" size="30" align="right" ></u--text>				
						</u-form-item>
						<u-form-item label="送货方式" labelWidth="160" prop="shipping_name" borderBottom="true" class="u-form-item" @click="shipping.isShow=true">
							<u-input v-model="form.shipping_name" placeholder="选择送货方式" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
						<u-form-item label="配送费支付方式" labelWidth="260" prop="payType_name" borderBottom="true" class="u-form-item" @click="payType.isShow=true">
							<u-input v-model="form.payType_name" placeholder="选择支付方式" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
						<template v-if="form.consignee.user_name">
							<u-form-item label="收货人" labelWidth="160" prop="payType_name" borderBottom="true" class="u-form-item" @click="select('address')">
								<u-input :value="form.consignee.user_name+' '+form.consignee.phone" placeholder="选择收货人" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
								<u-icon	slot="right" name="arrow-right"></u-icon>						
							</u-form-item>
							 
							<view class="address-area" >
								<view><u--textarea :value="form.consignee.address" autoHeight placeholder="" count border="none" disabled disabledColor="#ffffff" style="pointer-events: none;"></u--textarea></view>	
							</view>
						</template>
						<template v-else>
							<u-form-item label="收货人" labelWidth="160"  borderBottom="true" class="u-form-item" @click="select('address')">
								<view class="add-address" >选择收货人</view> 
								<u-icon	slot="right" name="arrow-right"></u-icon>						
							</u-form-item>
						</template>
						<u-form-item label="销售合同" labelWidth="160" prop="hetong" borderBottom="true" class="u-form-item" @click="hetonglist.isShow=true">
							<u-input v-model="form.hetong_name" placeholder="是否销售合同" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
					</u--form>
					 
				</view>
				
				<view v-if="form.hetong==1" class="goods-wrap m-b2">
					<view class="hd-title">
						<u--text class="" text="请上传销售合同" size="30" align="left" bold></u--text>  
					</view>	
					
					<c-uploadFile-hetong :currentfiles="form.hetongfiles" module="hetong"></c-uploadFile-hetong>
					
					<!-- <view style="padding: 0 20rpx;">
						<u-button type="primary" size="small" text="上传文件" @click="afterRead(2,$event)"></u-button>
					</view> -->
					
					<!-- <view class="upload-wrap">
						<u-upload
							width="180"
							height="180"
							:fileList="form.confiles"
							@afterRead="afterRead(2,$event)"
							@delete="deletePic"
							:previewFullImage="true"
							name="1"
							multiple
							:maxCount="10"
						></u-upload>
					</view> -->
					
				</view>
				 
				
				<view class="goods-wrap m-b2">
					<view class="hd-title">
						<u--text class="" text="申请原因" size="30" align="left" bold></u--text> 
					</view>	
					<u--textarea v-model="form.decript"  placeholder="请输入申请原因" count ></u--textarea>
				</view>
				
				<view class="goods-wrap m-b2">
					<view class="hd-title">
						<u--text class="" text="请上传付款凭证" size="30" align="left" bold></u--text> 
						<!-- <u--text v-if="form.shipping==2" class="" text="请拍摄物流单据及货物图片" size="30" align="left" bold></u--text> 
						<u--text v-if="form.shipping==3" class="" text="请拍摄物流单据及货物图片" size="30" align="left" bold></u--text> 
						<u--text v-if="form.shipping==4" class="" text="请拍摄车牌号及货物图片" size="30" align="left" bold></u--text> -->
					</view>	
					<view class="upload-wrap">
						<u-upload
							width="180"
							height="180"
							:fileList="form.fileList1"
							@afterRead="afterRead(1,$event)"
							@delete="deletePic"
							:previewFullImage="true"
							name="1"
							multiple
							:maxCount="10"
						></u-upload>
					</view>
					
				</view>
				
				<view class="goods-wrap"> 
					<u--form :model="form" ref="uForm" class="form-wrap"> 						 
						<u-form-item label="审批人" labelWidth="160" prop="apply_user_name" borderBottom="true" class="u-form-item" @click="applyuser.isShow=true">
							<u-input v-model="form.apply_user_name" placeholder="选择审批人" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
					</u--form> 
					<u--form :model="form" ref="uForm" class="form-wrap">
						<u-form-item label="抄送人" labelWidth="160" prop="form.applyCc_name" borderBottom="true" class="u-form-item" @click="showuser">
							<u-input v-model="form.applyCc_name" placeholder="选择抄送人" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
					</u--form> 
				</view> 
				  
			
			</template>
			
			<template v-if="page=='detail'">
				<u--form :model="form" ref="uForm" class="form-wrap">
					<u-form-item label="材料名称" labelWidth="160" prop="pname" borderBottom="true" class="u-form-item">
						<u-input v-model="form.pname" disabled disabledColor="#ffffff" placeholder="输入材料名称" border="none" />
					</u-form-item> 
					<u-form-item label="数量" labelWidth="160" prop="number" borderBottom="true" class="u-form-item">
						<u-input v-model="form.number" disabled disabledColor="#ffffff" placeholder="输入数量" border="none" />
					</u-form-item> 
					<u-form-item label="最小单位" labelWidth="160" prop="unit_name" borderBottom="true" class="u-form-item" >
						<u-input v-model="form.unit_name" disabled disabledColor="#ffffff" border="none" placeholder="选择单位" />									
					</u-form-item> 
					<u-form-item label="材料类别" labelWidth="160" prop="partstypename" borderBottom="true" class="u-form-item">
						<u-input v-model="form.partstypename" placeholder="选择材料类别" disabled disabledColor="#ffffff" border="none" />										
					</u-form-item> 
				</u--form>
			 		
			</template>
			
			<u-picker :show="pcategory.isShow" :columns="pcategory.data" keyName="name" :defaultIndex="[0]" itemHeight="90" :closeOnClickOverlay="true" @cancel="pcategory.isShow=false" @confirm="changepcategory" @close="pcategory.isShow=false"></u-picker> 
			
			<u-picker :show="company.isShow" :columns="company.data" keyName="short_name" :defaultIndex="company.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="company.isShow=false" @confirm="changepcompany" @close="company.isShow=false"></u-picker>
			
			<u-picker :show="hetonglist.isShow" :columns="hetonglist.data" keyName="name" :defaultIndex="[0]" itemHeight="90" :closeOnClickOverlay="true" @cancel="hetonglist.isShow=false" @confirm="changehetong" @close="hetonglist.isShow=false"></u-picker>
			
			<u-picker :show="pnames.isShow" :columns="pnames.data" keyName="name" :defaultIndex="[0]" itemHeight="90" :closeOnClickOverlay="true" @cancel="pnames.isShow=false" @confirm="changepnames" @close="pnames.isShow=false"></u-picker>			 
			
			<u-picker :show="units.isShow" :columns="units.data" keyName="uname" :defaultIndex="[0]" itemHeight="90" :closeOnClickOverlay="true" @cancel="units.isShow=false" @confirm="changeunits" @close="units.isShow=false"></u-picker>	
			
			<u-picker :show="projectlist.isShow" :columns="projectlist.data" keyName="name" :defaultIndex="projectlist.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="projectlist.isShow=false" @confirm="changeprojectlist" @close="projectlist.isShow=false"></u-picker>	
			
			<u-picker :show="payStatus.isShow" :columns="payStatus.data" keyName="name" :defaultIndex="payStatus.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="payStatus.isShow=false" @confirm="changepayStatus" @close="payStatus.isShow=false"></u-picker>
			
			<u-picker :show="shipping.isShow" :columns="shipping.data" keyName="name" :defaultIndex="shipping.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="shipping.isShow=false" @confirm="changeshipping" @close="shipping.isShow=false"></u-picker>
			
			<u-picker :show="payType.isShow" :columns="payType.data" keyName="name" :defaultIndex="payType.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="payType.isShow=false" @confirm="changepayType" @close="payType.isShow=false"></u-picker>
			
			<u-picker :show="address.isShow" :columns="address.data" keyName="user_name" :defaultIndex="address.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="address.isShow=false" @confirm="changeaddress" @close="address.isShow=false"></u-picker>
			 
			<u-picker :show="applyuser.isShow" :columns="applyuser.data" keyName="realname" itemHeight="90" :closeOnClickOverlay="true" @cancel="applyuser.isShow=false" @confirm="changeapplyuser" @close="applyuser.isShow=false"></u-picker>
 
		</view>  
		
		<c-footer > 
			<view slot="content" class="submit-btn-wrap">
				<view class="btn add-btn" @click="add">提交</view>
				<view class="btn catch-btn" @click="catchForm">存草稿</view>
			</view>
		</c-footer>
	</view>
</template>

<script>
	import {uploadToServe} from '@/common/util.js'
	import {IMG_PATH} from '../../config.js'
	export default {
		data() {
			return {
				id:'',
				apply_id:'',
				isLogin:true, 
				userInfo:uni.getStorageSync('userInfo'),
				isIphoneX:getApp().globalData.isIphoneX, 
				systemInfo:getApp().globalData.systemInfo,
				isShowSearch:false,
				type:'', 
				page:'',
				list:[], 
				isStop:true,
				form: { 
					proName:'',  
					company_name:'',
					goodsNum:0, 
					goods:[{
						categoryid:'',
						categoryname:'',
						p_id:'',
						pname:'',
						p_count:'',
						unit_id:'',
						unit_ids:'',
						unit:'',
						unit_list:[],
						one_price:'',
						stocks:[]
					}], 
					company_val:'',
					company_val_name:'',
					money:'',
					isPay:'',
					payStatus_name:'',
					customer:'',
					payStatus:'1',
					shipping:'',
					shipping_name:'',
					payType:'',
					payType_name:'',
					consignee:{},
					apply_user_id:'',
					apply_user_name:'', 
					fileList1:[],
					confiles:[],
					decript:'',
					applyCc:[],
					applyCc_name:'', 					 
					goodsIndex:0,
					hetong:0,
					hetong_name:'',
					hetongfiles:[]
				},
				projectlist:{
					isShow:false,
					data:[[
						{
							id:1,
							name:'市场项目'
						},
						{
							id:2,
							name:'政府项目'
						} 
					]],
					defaultIndex:[]
				},
				hetonglist:{
					isShow:false,
					data:[[
						{
							id:1,
							name:'有'
						},
						{
							id:0,
							name:'无'
						} 
					]],
					defaultIndex:[],
					
				},
				payStatus:{
					isShow:false,
					data:[[
						{
							id:1,
							name:'未结清'
						},
						{
							id:2,
							name:'已结清'
						},
						{
							id:3,
							name:'部分结清'
						} 
					]],
					defaultIndex:[]
				}, 
				shipping:{
					isShow:false,
					data:[[
						{
							id:1,
							name:'物流-自提'
						},
						{
							id:2,
							name:'物流-送货上门'
						},
						{
							id:3,
							name:'快递'
						},
						{
							id:4,
							name:'自行送货'
						}
						
					]],
					defaultIndex:[]
				}, 
				payType:{
					isShow:false,
					data:[[
						{
							id:1,
							name:'寄付'
						},
						{
							id:2,
							name:'到付'
						},
						{
							id:3,
							name:'部分付'
						} 
					]],
					defaultIndex:[]
				},
				pcategory:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},
				pnames:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},
				address:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},				 
				units:{					 
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},
				applyuser:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},	
				company:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},
				catch_order:uni.getStorageSync('copyOrder')
			}
		},
		async onLoad(params) {  
			if(params.apply_id){
				this.apply_id=params.apply_id;
			}
			this.type=params.type;
			this.page=params.page;   
			this.companylistsel();
			this.productstorehouse();
			this.productapplyuser();
			this.partstypes(); 
			this.getUserList(); 
			if(this.apply_id){
				this.catch_order=await this.getmedetails();  
				console.log('this.catch_order',this.catch_order)
				this.form.goods=[];				
				this.catch_order.product_list.forEach((item,index)=>{
					let good_item={};
					good_item.categoryid=item.pcategoryid;
					good_item.categoryname=item.cate_name;
					good_item.p_id=item.p_id;
					good_item.pname=item.pname;
					good_item.p_count=Number(item.p_count);
					good_item.unit_id=item.unit_id;
					good_item.unit_ids=item.pname;
					good_item.unit=item.uname; 
					good_item.unit_list=[];
					good_item.one_price=Number(item.one_price); 
					good_item.stocks=[];
					good_item.unit_ids=[];					
					if(item.m_unit.length>0){
						item.m_unit.forEach(j=>{
							good_item.unit_ids.push(j.id);
						})
					}
					this.form.goods.push(good_item);
					this.productalllist(index,good_item.categoryid);
				}) 
				this.form.goodsNum=this.catch_order.product_list.length-1;  
				if(this.catch_order.company_name){
					this.form.company_name=this.catch_order.company_name;
				}else{
					this.company.data[0].forEach(i=>{
						if(i.value==this.catch_order.company_val){
							this.form.company_val=this.catch_order.company_val;
							this.form.company_val_name=i.label;
						}
					})
				}
				
				this.payStatus.data[0].forEach(i=>{
					if(i.id==this.catch_order.project_status){
						this.form.payStatus=this.catch_order.project_status;
						this.form.payStatus_name=i.name;
					}
				})
				
				this.shipping.data[0].forEach(i=>{
					if(i.id==this.catch_order.sendproduct[0].send_type){
						this.form.shipping=this.catch_order.sendproduct[0].send_type;
						this.form.shipping_name=i.name;
					}
				})
				
				this.payType.data[0].forEach(i=>{
					if(i.id==this.catch_order.sendproduct[0].pay_type){
						this.form.payType=this.catch_order.sendproduct[0].pay_type;
						this.form.payType_name=i.name;
					}
				})				
				if(this.catch_order.files_path.length>0){
					this.catch_order.files_path.forEach(i=>{
						i.file_name=i.files_path.split('/')[i.files_path.split('/').length-1];
						if(i.file_type==2){
							let item={
								status: 'success',
								message: '',
								url: i.files_path,
								name:'',
								file_name:i.file_name
							}
							this.form.fileList1.push(item)
						} 
						if(i.file_type==10){
							this.form.hetongfiles.push(i);
						}
					}) 
				} 
				if(this.form.hetongfiles.length>=0){
					this.form.hetong=1;
					this.form.hetong_name='有';					
				} 
				this.form.consignee={ 
					"id": this.catch_order.sendproduct[0].cust_id,
					"default": this.catch_order.sendproduct[0].default,
					"user_name": this.catch_order.sendproduct[0].NAME,
					"phone": this.catch_order.sendproduct[0].phone,
					"address": this.catch_order.sendproduct[0].address
				}  
				this.form.money=this.catch_order.product_money;
				this.form.isPay=this.catch_order.payments; 
				this.form.proName=this.catch_order.projectname; 
				this.form.customer=this.catch_order.customer; 
				this.form.decript=this.catch_order.apply_reason;
			} 
			
			if(uni.getStorageSync('catch_chuku')){ 
				this.form=uni.getStorageSync('catch_chuku');
				console.log(this.form)
				if(this.form.goods.length>0){
					this.form.goods.forEach(async (item,index)=>{						 
						await this.productalllist(index,item.categoryid);
					}) 
				}  
			}
		},
		onShow() {
			
		}, 
		onHide() {
			 
		},
		onUnload() {			
			
		},
		methods: {  
			setPrice(){
				let money=0;
				this.form.goods.forEach(i=>{
					console.log(i)
					money+=(Number(i.one_price)*Number(i.p_count));
					
				})
				this.form.money=money;
				console.log('this.form.money',money,this.form.money)
			},
			catchForm(){
				uni.setStorage({
					key:'catch_chuku',
					data:this.form
				})
				this.$showToast({
					title:'保存草稿成功' 
				})
			},
			
			async getmedetails(){
				let res=await this.$api.user_api.medetails({
					notice_id:this.id,
					apply_id:this.apply_id
				});    
				if(res.data.code==200){  
					this.isshow=true;
					if(res.data.data.m.files_path.length>0){
						res.data.data.m.files_path.forEach(item=>{
							item.files_path=IMG_PATH+item.file_path+'/'+item.files_path; 
						})
					} 
					return res.data.data.m;
					
				}
			}, 
			
			async companylistsel(){ 
				let res=await this.$api.user_api.companylistsel({
					group_id:'-1',
					keyword:''
				});     
				if(res.data.code==200){   
					res.data.data.push({
						"value": "-1",
						"label": "其他",
						"short_name": "其他"
					})					
					this.$set(this.company.data,0,res.data.data);     
				}
			}, 
			
			changepcompany(e){
				if(e.value[0].value==-1){
					this.form.company_val_name=e.value[0].short_name;
					this.form.company_val='';
				}else{
					this.form.company_val_name=e.value[0].short_name;
					this.form.company_val=e.value[0].value;
				}
				
				this.company.isShow=false;
				this.company.defaultIndex=e.indexs;
			},
			
			changehetong(e){
				this.form.hetong_name=e.value[0].name;
				this.form.hetong=e.value[0].id;
				this.hetonglist.isShow=false;
				this.hetonglist.defaultIndex=e.indexs;
			},
			jumpAdd(){
				uni.navigateTo({
					url:'/package_kucun/address/address?type=add'
				})
			},
			
			select(type){
				
				if(type=='category'){
					uni.navigateTo({
						url:`/package_kucun/list-select/list-select?index=${this.form.goodsIndex}&page=category`
					})
				}
				
				if(type=='product'){
					uni.navigateTo({
						url:`/package_kucun/list-select/list-select?categoryid=${this.form.goods[this.form.goodsIndex].categoryid}&index=${this.form.goodsIndex}&page=product`
					})
				}
				
				if(type=='address'){
					uni.navigateTo({
						url:'/package_kucun/address/address?type=select'
					})
				}				
			},
			
			 
			
			// 删除图片
			deletePic(event) {
				this.form.fileList1.splice(event.index, 1) 
			},
			
			// 新增图片
			async afterRead(type,event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let api_url='/api/product/uploadfile';
				let fileListLen = 0;
				let apply_type='';
				if(type==1){
					apply_type=1;
					fileListLen = this.form.fileList1.length;
					lists.map((item) => {
						this.form.fileList1.push({
							...item,
							status: 'uploading',
							message: '上传中'
						}) 
					})
				}				 				
				
				for (let i = 0; i < lists.length; i++) { 
					uploadToServe({
						url: api_url,
						filePath: lists[i].url,
						name: 'fileinput',
						formData: {
							apply_id:'',
							apply_type:apply_type   // 1入库,2出库,3发货,4（货款）结清  10 合同
						}
					}).then((res)=>{ 
						if(type==1){
							let item = this.form.fileList1[fileListLen];
							this.form.fileList1.splice(fileListLen, 1, Object.assign(item, {
								status: 'success',
								message: '',
								url: `${IMG_PATH}${res.data.file_path}/${res.data.file_name}`,
								name:res.data.file_name
							}))
						}						
						fileListLen++ 
					}) 
				}
			}, 
			
			 
						
			async addGoods(){
				this.form.goodsNum+=1; 
				let data={
					categoryid:'',
					categoryname:'',
					p_id:'',
					pname:'',
					p_count:'',
					unit_id:'',
					unit:'',
					unit_list:[],
					unit_ids:'', 
					one_price:'',
					stocks:[]
				};
				this.$set(this.form.goods,this.form.goodsNum,data);
				 
				
			},
			
			async getUserList() {
				let res = await this.$api.user_api.userListBydep({ 
					keywords: '',
					department_id:''
				})     
				if (res.data.code == 200) { 
					let applyCc_name = [],
						applyCc=[];
					res.data.data.forEach(item=>{						
						if(this.userInfo.department_id==item.department_id){
							item.ulist.forEach(subitem=>{
								if(subitem.dep_leader==1){									
									applyCc_name.push(subitem.username);
									applyCc.push(subitem.id);  
								} 
							}) 
						} 
						if(item.department_id==29){
							item.ulist.forEach(subitem=>{ 
								applyCc_name.push(subitem.username);
								applyCc.push(subitem.id); 
							}) 
						} 
						if(item.department_id==31){
							item.ulist.forEach(subitem=>{ 
								if(subitem.id==28||subitem.id==45){
									applyCc_name.push(subitem.username);
									applyCc.push(subitem.id); 
								}								
							}) 
						}
						this.form.applyCc_name = applyCc_name.join(',');
						this.form.applyCc=applyCc;
					})  
				}
			},
			
			showuser(){ 
				uni.navigateTo({
					url:'/package_msg/mailUser/mailUser?scene=6&type=2&select_data='+this.form.applyCc.join(',')				
				})
			},
			
			delGoods(idx){
				this.form.goodsNum-=1;				 
				this.form.goods.splice(idx,1);
			},
			 
			async getaddress(){ 
				let res=await this.$api.user_api.memecustinfo({ 
					page:1,
					limit:10000,
					search:''
				});  
				if(res.data.code==200){	
					if(res.data.data.length>0){
						res.data.data.forEach(item=>{
							 item.name=item.user_name;
						})
						// this.$set(this.address.data,0,res.data.data);
						// this.form.consignee=this.address.data[0][0];
					}					
				}
			},   
			
			async add(){
				
				let {proName,customer,decript,apply_user_id,company_name,money,isPay,payStatus,shipping,payType,apply_user_name,company_val}=this.form;
				console.log(this.form)
				let products=[];
				let files=[];
				let con_files=[];
				let totalstock=0; 
				
				let flag=this.form.fileList1.some(item => item.status === 'uploading');  
				if(!flag){
					uni.showLoading({
						title:'正在上传图片',
					})
				}else{
					uni.hideLoading()
				}
				 
				this.form.fileList1.forEach(item=>{
					files.push(item.name)
				})  
				
				this.form.confiles.forEach(item=>{
					con_files.push(item.file_name)
				})   
				
				let others={
					proName,
					company_name,
					company_val,
					customer,
					money,
					isPay,
					payStatus,
					shipping,
					payType,
					apply_user_id,
					apply_user_realname:apply_user_name,
					decript,
					files:files.join(','),
					iscont:this.form.hetong,
					cont_file:con_files.join(','),
					applyCc:this.form.applyCc,
					consignee:this.form.consignee,
				};
				this.form.goods.forEach(item=>{
					let g_item={}; 
					g_item.p_id=item.p_id;
					g_item.pname=item.pname; 
					g_item.p_count=item.p_count; 
					g_item.unit=item.unit;
					g_item.unit_id=item.unit_id;
					g_item.one_price=item.one_price;
					products.push(g_item)
					item.stocks.forEach(subitem=>{
						totalstock+=Number(subitem.p_count)
					})
				}) 
				 
				let params={
					products:JSON.stringify(products),
					others:JSON.stringify(others),					 
				} 
				
				  
				for(let i=0;i<this.form.goods.length;i++){					
					// if(this.form.goods[i].stocks.length==0||this.form.goods[i].p_count>totalstock){						
					// 	this.$showToast({
					// 		title:this.form.goods[i].pname+'库存不足' 
					// 	})
					// 	return
					// }
					if(this.form.goods[i].p_count==''){
						this.$showToast({
							title:'请填写完整成品信息' 
						})
						return
					} 
					if(Number(this.form.goods[i].p_count)<=0){
						this.$showToast({
							title:'出库数量必须大于0' 
						})
						return
					} 
				}
				 
				if(customer==''){
					this.$showToast({
						title:'请输入客户名称' 
					})
					return
				}
				// if(money==''){
				// 	this.$showToast({
				// 		title:'请输入总货款' 
				// 	})
				// 	return
				// }
			 
				if(isPay==''){
					this.$showToast({
						title:'请输入已结款' 
					})
					return
				}
				if(Number(isPay)<0){
					this.$showToast({
						title:'已结款必须大于0' 
					})
					return
				}  
				if(this.form.hetong==1&&this.form.confiles.length==0){
					this.$showToast({
						title:'请上传合同' 
					})
					return
				}  
				if(this.isStop){
					this.isStop=false;
					let res=await this.$api.user_api.outsave(params);
					if(res.data.code==200){
						this.$showToast({
							icon:'success',
							title:res.data.msg,
							duration:1000,
						})  
						uni.removeStorageSync('catch_chuku');
						setTimeout(()=>{ 
							this.isStop=true;
							uni.navigateBack({
								delta:1
							})
						},300)						
					}else{
						this.isStop=true;
						this.$showToast({
							icon:'error',
							title:res.data.msg,
							duration:1000,
						}) 
					}
				}
				
				
				 
			},
			
			changeprojectlist(e){
				this.form.proName=e.value[0].name; 
				this.projectlist.isShow=false;
				this.projectlist.defaultIndex=e.indexs;
			}, 
			 
			changepayStatus(e){
				this.form.payStatus_name=e.value[0].name;
				this.form.payStatus=e.value[0].id;
				this.payStatus.isShow=false;
				this.payStatus.defaultIndex=e.indexs;
			},
			
			changeaddress(e){
				this.form.consignee=e.value[0];
				this.address.isShow=false;
			},
			
			changepayType(e){
				this.form.payType_name=e.value[0].name;
				this.form.payType=e.value[0].id;
				this.payType.isShow=false;
				this.payType.defaultIndex=e.indexs;
			},
			
			changeshipping(e){
				this.form.shipping=e.value[0].id; 
				this.form.shipping_name=e.value[0].name; 
				this.shipping.isShow=false;
				this.shipping.defaultIndex=e.indexs;
			},
			
			changepnames(e){  
				let idx=this.form.goodsIndex;		 
				this.form.goods[idx].pname=e.value[0].name;
				this.form.goods[idx].p_id=e.value[0].id;	
				this.form.goods[idx].unit_ids=e.value[0].unit_id;		 
				
				this.outstock(idx,this.form.goods[idx].p_id,this.form.goods[idx].unit_ids); 
				this.$set(this.units.data,0,this.form.goods[idx].unit_list);   
				this.pnames.isShow=false;  
				
			}, 
			changepcategory(e){
				let idx=this.form.goodsIndex;
				this.form.goods[idx].categoryid=e.value[0].id;
				this.form.goods[idx].categoryname=e.value[0].name; 
				this.form.goods[idx].pname='';
				this.form.goods[idx].p_id='';
				this.pcategory.isShow=false;
				// this.productalllist(idx,this.form.goods[idx].categoryid);
			},
			
			async productcategorylist(){ 
				let res=await this.$api.user_api.productcategorylist({
					keyword:''
				});    
				if(res.data.code==200){  
					this.$set(this.pcategory.data,0,res.data.data);  
					// this.form.goods[0].categoryname=res.data.data[0].name;
					// this.form.goods[0].categoryid=res.data.data[0].id;
					// this.productalllist(0,this.form.goods[0].categoryid);  
				}
			}, 
			 
			changeunits(e){ 
				
				let idx=this.form.goodsIndex; 
				let stocks=this.form.goods[idx].stocks;  // 
				stocks=stocks.concat(this.form.goods[idx].stocks);   
				this.form.goods[idx].unit=e.value[0].uname;
				this.form.goods[idx].unit_id=e.value[0].id; 
				this.form.goods[idx].stocks.map(item=>{
					item.count=item.p_count/e.value[0].num,
					item.uname=e.value[0].uname
				}) 
				// this.form.goods[idx].update_stocks=this.form.goods[idx].stocks;
				this.units.isShow=false;
			},
			
			async productalllist(idx,cid){
				let res=await this.$api.user_api.outgetproducts({
					id:cid, 
				});     
				if(res.data.code==200){  
					this.$set(this.pnames.data,0,res.data.data); 
					if(this.apply_id){ 
						this.form.goods.forEach((i,k)=>{
							this.outstock(k,this.form.goods[k].p_id,this.form.goods[k].unit_ids);
						})
					} 
				}
			}, 
			
			async outstock(idx,pid,uids){  
				let res=await this.$api.user_api.outstock({
					pid:pid,
					unit_id:uids
				});     
				console.log(res)
				if(res.data.code==200){ 	
					res.data.data.stocks.forEach(item=>{
						item.count=item.p_count;
					}) 
					if(this.apply_id){
						this.form.goods[idx].stocks=res.data.data.stocks;
						this.$set(this.units.data,0,res.data.data.units);  
						res.data.data.units.forEach(i=>{
							if(this.form.goods[idx].unit_id==i.id){
								this.form.goods[idx].stocks.map(item=>{
									item.count=item.p_count/i.num,
									item.uname=i.uname
								}) 
							}
						}) 
						// this.form.goods[idx].unit_id=res.data.data.units[0].id;
						// this.form.goods[idx].unit=res.data.data.units[0].uname;
						// this.form.goods[idx].unit_list=res.data.data.units;  
					}else{
						this.form.goods[idx].stocks=res.data.data.stocks;
						this.$set(this.units.data,0,res.data.data.units);  					 
						this.form.goods[idx].unit_id=res.data.data.units[0].id;
						this.form.goods[idx].unit=res.data.data.units[0].uname;
						this.form.goods[idx].unit_list=res.data.data.units;  
					}
					
				}
			}, 
			
			
			
			async productstorehouse(){
				let res=await this.$api.user_api.productstorehouse();     
				if(res.data.code==200){ 
					this.$set(this.storehouse.data,0,res.data.data); 
					this.form.storehouse_id=res.data.data[0].id;
					this.form.storehouse_name=res.data.data[0].sname;
					this.form.storehouse_uid=res.data.data[0].storehouseman;
				}
			}, 
			
			changestorehouse(e){
				this.form.storehouse_name=e.value[0].sname;
				this.form.storehouse_id=e.value[0].id; 
				this.form.storehouse_uid=e.value[0].storehouseman;
				this.storehouse.isShow=false;
				this.storehouse.defaultIndex=e.indexs;
			},
			
			async productapplyuser(){
				let res=await this.$api.user_api.productapplyuser({
					pid:'0',
					process_type:'2' 
				});      
				if(res.data.code==200){ 
					this.$set(this.applyuser.data,0,res.data.data.apply_user_list);
					this.form.apply_user_id=this.applyuser.data[0][0].uid;
					this.form.apply_user_name=this.applyuser.data[0][0].realname;
					
					let new_data = [],
						new_data_id=[];
					res.data.data.cc_user_list.forEach(item => {
						new_data.push(item.realname);
						new_data_id.push(item.uid);
					}) 
					this.form.applyCc_name = new_data.join(',');
					this.form.applyCc=new_data_id;
					
				}
			}, 
			
			changeapplyuser(e){
				this.form.apply_user_name=e.value[0].realname;
				this.form.apply_user_id=e.value[0].uid; 
				this.applyuser.isShow=false;
				this.applyuser.defaultIndex=e.indexs;
			},
			
			
			changepartstype(e){
				let idx=this.form.goodsIndex;
				this.form.goods[idx].p_id=e.value[0].id;
				this.form.goods[idx].p_name=e.value[0].cname; 
				this.partstype.isShow=false; 
				this.subpartstypes(this.form.goods[idx].p_id);
			}, 
			
			async partstypes(){
				let res=await this.$api.user_api.partstypes(); 
				if(res.data.code==200){  
					this.$set(this.partstype.data,0,res.data.data);  
					this.subpartstypes(this.form.goods[idx].p_id); 
				}
			},  
			
			async subpartstypes(cid){
				let res=await this.$api.user_api.partsalllist({
					typeid:cid,
					is_page:''
				});   
				if(res.data.code==200){  
					if(res.data.data.length>0){
						this.$set(this.gnames.data,0,res.data.data); 
						let idx=this.form.goodsIndex;
						this.form.goods[idx].pname_id=this.gnames.data[0][0].id;
						this.form.goods[idx].pname=this.gnames.data[0][0].name;
						this.form.goods[idx].store_count=this.gnames.data[0][0].number;
						this.form.goods[idx].unit=this.gnames.data[0][0].uname; 
						this.form.goods[idx].price=this.gnames.data[0][0].price; 
					}					
				}
			}, 
			
			changegnames(e){ 
				let idx=this.form.goodsIndex;
				this.form.goods[idx].pname_id=e.value[0].id;
				this.form.goods[idx].pname=e.value[0].name; 
				this.form.goods[idx].store_count=e.value[0].number;
				this.form.goods[idx].unit=e.value[0].uname; 
				this.form.goods[idx].price=e.value[0].price; 
				this.gnames.isShow=false; 
			},
			
			
			
			
			 
		},
		computed:{ // 抄送人
			cs_user() {
				return this.$store.getters.getCsUserState;
			},
			
			select_address(){
				return this.$store.state.select_address_update
			},
			
			hetong(){				
				return this.$store.state.hetong
			},
			
			product_update(){
				return this.$store.state.product_update
			},
			
			category_update(){
				return this.$store.state.category_update
			}
			
			
		},
		watch:{ 
			// 监听抄送人
			cs_user(newer, older) { 
				if (newer.length > 0) { 
					let new_data = [],
						new_data_id=[];
					newer.forEach(item => {
						new_data.push(item.username);
						new_data_id.push(item.id);
					})
					this.form.applyCc_name = new_data.join(',');
					this.form.applyCc=new_data_id;
				} else {
					this.form.applyCc_name = '';
					this.form.applyCc=[]; 
				}  
			},
			
			select_address(newer,older){ 
				if(newer!=older){
					this.form.consignee=newer; 
				}
			},
			 
			
			'form.goodsIndex': {
				handler(newer, older) { 
				  // this.productalllist(newer,this.form.goods[newer].categoryid);
				},
				immediate: true, 
			},
			 
			hetong(newer,older){ 
				if(newer!=older){
					this.form.confiles=[];
					this.form.confiles=newer;
				}
			},
			
			category_update(n,o){
				if(n!=o){ 
					let idx=n.index;
					let item=n.data;
					this.form.goods[idx].categoryname=item.name;
					this.form.goods[idx].categoryid=item.id; 
					this.form.goods[idx].pname='';
					this.form.goods[idx].p_id='';	
					this.form.goods[idx].unit_ids='';
					this.form.goods[idx].p_count='';
				}
			},
			
			product_update(n,o){
				if(n!=o){ 
					let idx=n.index;
					let item=n.data;  
					this.form.goods[idx].pname=item.name;
					this.form.goods[idx].p_id=item.id;	
					this.form.goods[idx].unit_ids=item.unit_id;	 
					this.outstock(idx,this.form.goods[idx].p_id,this.form.goods[idx].unit_ids);
					
				}
			}
			
		}
	}
</script>

<style lang="less" >
	.hd-title{
		display: flex;
		justify-content: space-between;
		padding: 20rpx;
	}
	.add-address{
		color: #09f;
	}
	.address-area{ 
		width: 100%;
	}
	 
	.store-info{
		padding:20rpx;
		color:green;
		.text-label{
			width: 120rpx;
		}
		.text-unit{
			flex-grow: 2;   
		}
	}
	.upload-wrap{
		padding:20rpx;		
		background: #fff;
	}
	.u-form-item{
		.text-inp{
			width: 150rpx;
			flex-shrink: 0;
		}
		.text-unit{ 
			flex-grow: 2; 
			
		}
	}
	.num-inp-bar{
		width: 80%;		
		justify-content: space-between;
	}
	.form-wrap{
		padding:0 20rpx; 
		.row-2{
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}
	.slot-content{
		.hd{
			padding: 30rpx 0 10rpx;
			font-size: 30rpx;
		}
	}
	.popup-form-wrap{
		padding:20rpx;
		border-radius: 15rpx;		 
	}
	.select-pz-wrap{
		display: flex;
		flex-wrap: wrap;
		.item{
			position: relative; 
			margin:0 20rpx 20rpx 0;
			.del-icon{
				position: absolute;
				right: -15rpx;
				top: -15rpx;
			}
		}
	}
	.submit-btn-wrap{
		display: flex;
		.btn{
			height: 36px;
			line-height: 36px; 
			margin: 5px 10rpx;
			border-radius: 8rpx;
			color: #fff;
			font-size: 30rpx;
			text-align: center;
		}
		.add-btn{	
			flex-grow: 2;
			background: #1cab1b;		
		} 
		.catch-btn{
			width: 150rpx;
			background: #09f;
		}
	}
	 
	.partslist-popup{ 
		position: relative;
		height: 100%;
		padding: 0 40rpx; 
		.hd{ 
			height: 40px;
		}
		.select-wrap{
			height: 80%;
			flex-grow: 2;
			margin: 20rpx 0;
			overflow-y: auto; 
			.item{
				margin-bottom: 20rpx;
			}
		}
		.btn-wrap{  
			margin: 20rpx 0;
			.btn{
				margin: 0 30rpx;
			}
		}
	}
</style>
